<template>
    <el-form :model="formData" :rules="formRules" ref="formRef" label-width="90px" label-position="left">
        <el-form-item label="名　　称：" prop="name">
            <el-input v-model="formData.name" @keyup.enter.native="enterKey($event, 'L1')"></el-input>
        </el-form-item>
        <el-form-item label="备　　注：" prop="remark">
            <el-input v-model="formData.remark" ref="L1" @keyup.enter.native="save('formRef')"></el-input>
        </el-form-item>
        <el-form-item label="连续增加：" style="margin-bottom: 10px !important;" v-if="!rowId">
            <el-checkbox v-model="continueAdd"></el-checkbox>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" size="small" @click="save('formRef')">保存</el-button>
            <el-button type="danger" size="small" @click="close">关闭</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
export default {
    data() {
        return {
            continueAdd: false,         //是否连续新增
            formData: {
                lock: true
            },
            formRules: {
                name: [{required: true, message: '名称不能为空', triggle: 'blur'}]
            },
            rowId: this.$store.state.settlementWayStore.rowId
        }
    },
    computed: {
        systemId() {
            return this.$store.state.systemId;
        }
    },
    methods: {
        getUpdateData() {           //获取修改数据
            this.$http.get('closeAccount/findCloseAccountById?id=' + this.rowId).then(res => {
                if(res.data.code == 20001) {
                    if(res.data.data) {
                        res.data.data['lock'] = Boolean(res.data.data['lock']);
                        this.formData = res.data.data;
                    }
                }
            });
        },
        save(formName) {            //保存
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let data = {
                        name: this.formData['name'],
                        remark: this.formData['remark'],
                        lock: Number(this.formData['lock'])
                    }
                    if(this.formData['id']) {
                        //修改
                        data['id'] = this.formData['id'];
                        this.$http({
                            url: 'closeAccount/updateCloseAccount',
                            method: 'post',
                            params: data
                        }).then(res => {
                            if(res.data.code == 20001) {
                                this.$message({message: '修改成功', type: 'success', duration: 1500});
                                setTimeout(() => {
                                    this.$store.dispatch('settlementWayStore/getTableData');
                                    this.close();
                                }, 1000);
                            } else {
                                this.$message({message: res.data.msg || '修改失败', type: 'error', duration: 1500});
                            }
                        });
                    } else {
                        //新增
                        data['systemId'] = this.systemId;
                        this.$http({
                            method: "post",
                            url: "closeAccount/insertCloseAccount",
                            params: data
                        }).then(res => {
                            if(res.data.code == 20001) {
                                this.$message({message: '新增成功', type: 'success', duration: 1500});
                                setTimeout(() => {
                                    this.continueAdd ? this.$refs[formName].resetFields() : this.close();
                                    this.$store.dispatch('settlementWayStore/getTableData');
                                }, 1000);
                            } else {
                                this.$message({message: res.data.msg || '新增失败', type: 'error', duration: 1500});
                            }
                        });
                    }
                }
            });
        },
        close() {           //关闭弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        },
        enterKey(lastRef, nextRef) {		//回车键换行
            if(lastRef.target.value) {
                this.$refs[nextRef].focus();
            }
        }
    },
    created() {
        if(this.rowId) this.getUpdateData();
    }
}
</script>